package com.zzl.finance.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzl.finance.data.constant.StringConstant;
import com.zzl.finance.data.dao.EnterpriseFinancialProductDao;
import com.zzl.finance.data.dto.EnterpriseFinancialProductAddDto;
import com.zzl.finance.data.dto.EnterpriseFinancialProductDto;
import com.zzl.finance.data.dto.EnterpriseFinancialProductUpdateDto;
import com.zzl.finance.data.entity.FinancialProductRateEntity;
import com.zzl.finance.data.service.EnterpriseFinancialProductService;
import com.zzl.general.common.constant.Constant;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.QmBeanUtils;
import com.zzl.general.common.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 金融产品
 *
 * @author wztsl
 * @date 2021-12-22 19:40:27
 */
@Slf4j
@Service("enterpriseFinancialProductService")
public class EnterpriseFinancialProductServiceImpl extends ServiceImpl<EnterpriseFinancialProductDao, FinancialProductRateEntity> implements EnterpriseFinancialProductService {
    @Autowired
    private EnterpriseFinancialProductService enterpriseFinancialProductService;

    /**
     * 分页查询企业服务费配置
     *
     * @param params 筛选条件
     * @return 企业服务费配置
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //排序字段：create_time
        String pageField = (String) params.get(Constant.PAGE);
        //排序方式 order:  desc asc
        String order = (String) params.get(Constant.ORDER);
        IPage<EnterpriseFinancialProductDto> page = new Query<EnterpriseFinancialProductDto>()
                .getPage(params, StringUtils.isBlank(pageField)
                        ? StringConstant.CREATE_TIME_FILED : pageField, Constant.ASC.equals(order));
        String financialProductId = (String) params.get("financialProductId");
        //financialProductId为空，查询所有企业；不为空查询所有企业以及已设置的服务费
        IPage<EnterpriseFinancialProductDto> result = this.baseMapper.queryEnterprisePage(page, params);
        List<EnterpriseFinancialProductDto> records = result.getRecords();
        if (!CollectionUtils.isEmpty(records) && !StringUtils.isBlank(financialProductId)) {
            List<String> enterpriseIdList = records.stream().map(EnterpriseFinancialProductDto::getEnterpriseId).collect(Collectors.toList());
            QueryWrapper<FinancialProductRateEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(FinancialProductRateEntity::getEnterpriseId, enterpriseIdList)
                    .eq(FinancialProductRateEntity::getFinancialProductId, Integer.parseInt(financialProductId));
            List<FinancialProductRateEntity> list = enterpriseFinancialProductService.list(queryWrapper);
            if (!CollectionUtils.isEmpty(list)) {
                Map<String, FinancialProductRateEntity> collect = list.stream().collect(Collectors.toMap(FinancialProductRateEntity::getEnterpriseId, Function.identity(), (key1, key2) -> key2));
                records.forEach(x -> {
                    FinancialProductRateEntity enterpriseFinancialProductEntity = collect.get(x.getEnterpriseId());
                    if (!ObjectUtils.isEmpty(enterpriseFinancialProductEntity)) {
                        x.setId(enterpriseFinancialProductEntity.getId());
                        x.setFinancialProductId(enterpriseFinancialProductEntity.getFinancialProductId());
                        x.setPlatServiceRate(enterpriseFinancialProductEntity.getAnnualizedInterestRate());
                    }
                });
            }
        }

        return new PageUtils(result);
    }

    /**
     * 保存企业融资费率
     *
     * @param list 入参
     */
    @Override
    public void saveEnterpriseFinancialProduct(List<EnterpriseFinancialProductAddDto> list, String financialProductId) throws SysException {
        List<FinancialProductRateEntity> entityList =
                QmBeanUtils.copyListProperties(list, FinancialProductRateEntity::new);
        entityList.forEach(x -> x.setFinancialProductId(financialProductId));
        if (!this.saveBatch(entityList)) {
            throw new SysException("企业融资费率保存失败");
        }
    }

    /**
     * 新增或修改企业平台服务费
     *
     * @param list 入参
     */
    @Override
    public void saveOrUpdateProduct(List<EnterpriseFinancialProductUpdateDto> list, String financialProductId) throws SysException {
        List<FinancialProductRateEntity> entityList =
                QmBeanUtils.copyListProperties(list, FinancialProductRateEntity::new);
        entityList.forEach(x -> x.setFinancialProductId(financialProductId));
        if (this.saveOrUpdateBatch(entityList)) {
            throw new SysException("企业融资费率修改失败");
        }

    }
}